﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Globalization;

namespace University.Workshop.Presenters.AppointmentFolder
{
    public class CalendarPresenter
    {
        private ICalendarView view;
        private DateTime currentShowingDate;
        private DateTime todayDate;

        public CalendarPresenter(ICalendarView view)
        {
            if (view != null)
            {
                this.view = view;
                todayDate = DateTime.Now;
                currentShowingDate = todayDate;
                view.TodayDate = todayDate;
                view.CurrentShowingDate = currentShowingDate;
                UpdateDayAppointments();
            }
        }

        public void GetCurrentMonth()
        {
            string month="";

            switch (currentShowingDate.Month)
            {
                case 1: month = "January"; break;
                case 2: month = "February"; break;
                case 3: month = "March"; break;
                case 4: month = "April"; break;
                case 5: month = "May"; break;
                case 6: month = "June"; break;
                case 7: month = "July"; break;
                case 8: month = "August"; break;
                case 9: month = "September"; break;
                case 10: month = "October"; break;
                case 11: month = "November"; break;
                case 12: month = "December"; break;
                default: month = ""; break;
            }
            view.SetCurrentCalendar(CalendarManager.GetCalendarByMonth(month));
        }

        public void GetNextMonth()
        {
            currentShowingDate = new DateTime(currentShowingDate.Year,
                                              currentShowingDate.Month+1,
                                              1);

            view.CurrentShowingDate = currentShowingDate;
            GetCurrentMonth();
        }

        public void GetPreviousMonth()
        {
            currentShowingDate = new DateTime(currentShowingDate.Year,
                                              currentShowingDate.Month - 1,
                                              1);
            view.CurrentShowingDate = currentShowingDate;
            GetCurrentMonth();
        }

        public void UpdateSelectedDay(string selectedDay)
        {
            int selectedDayInt=1;
            try
            {
                if (selectedDay != null)
                {
                    if (!selectedDay.Equals(" "))
                    {

                        selectedDayInt = int.Parse(selectedDay, CultureInfo.InvariantCulture);


                        currentShowingDate = new DateTime(currentShowingDate.Year,
                                                          currentShowingDate.Month,
                                                          selectedDayInt);
                        view.CurrentShowingDate = currentShowingDate;
                        UpdateDayAppointments();

                    }
                }
            }
            catch (NullReferenceException)
            { }
        }

        public void UpdateDayAppointments()
        {
            view.SetDayAppointments(Business.AppointmentManager.GetAppointmentsByDay(currentShowingDate.ToShortDateString()));
        }
    }
}
